package com.qzone;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class CommonUtil {
	static AlertDialog alertDialog;
	static SharedPreferences sharedPreferences;
	public static final String MyPREFERENCES = "QZone" ;
	public static final String GCMREGID = "gcmregid" ;
	static GoogleCloudMessaging gcm;
    static String regid;
    static String PROJECT_NUMBER = "972754813927";	
    public static int notifyid=1;
	public static void deleteStudent(final Context mContext,final String MasterId)
	{
		final DBHelper dbHelper=new DBHelper(mContext);
		AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
		builder.setMessage(R.string.delete_confirm);
		builder.setPositiveButton(R.string.delete_conf, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dbHelper.deleteRecord(MasterId);
				Intent intent;
	        	if(dbHelper.getNumRows()>0)
	        	{
	        		intent=new Intent(mContext,StudentList.class);	
	        	}
	        	else
	        	{
	        		intent=new Intent(mContext,AddStudent.class);
	        	}
	        	mContext.startActivity(intent);
	        	((Activity) mContext).finish();
			}
		});
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				alertDialog.dismiss();
			}
		});
		alertDialog = builder.create();
	      alertDialog.show();
	}
	
	  public static long ipToLong(String ipAddress) {
		  
			String[] ipAddressInArray = ipAddress.split("\\.");
		 
			long result = 0;
			for (int i = 0; i < ipAddressInArray.length; i++) {
		 
				int power = 3 - i;
				int ip = Integer.parseInt(ipAddressInArray[i]);
				result += ip * Math.pow(256, power);
		 
			}
		 
			return result;
		  }
	  
	  public static String longToIp(long ip) {
		  
			return ((ip >> 24) & 0xFF) + "." 
				+ ((ip >> 16) & 0xFF) + "." 
				+ ((ip >> 8) & 0xFF) + "." 
				+ (ip & 0xFF);
		 
		  }
	  
	  public static String getPref(Context mContext,String key)
	  {
		  sharedPreferences = mContext.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
		  if(sharedPreferences.contains(key))
		  {
			  return sharedPreferences.getString(key, "");
		  }
		  else
		  {
			  return "";
		  }
	  }
	  public static void setPref(Context mContext,String key,String value)
	  {
		  sharedPreferences = mContext.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
		  Editor editor = sharedPreferences.edit();
		  editor.putString(key, value);
		  editor.commit();
		  
	  }
	  
	  public static void getRegId(final Context mContext){
	    	new AsyncTask<Void, Void, String>() {
	            @Override
	            protected String doInBackground(Void... params) {
	                try {
	                    if (gcm == null) {
	                        gcm = GoogleCloudMessaging.getInstance(mContext);
	                    }
	                    regid = gcm.register(PROJECT_NUMBER);
	                    Log.d("GCMMM",  regid);

	                   
	                } catch (IOException ex) {
	                    Log.i("GCM",  ex.getMessage());
	                    
	                }
	                return regid;
	            }

	            @Override
	            protected void onPostExecute(String regid) {
	            	CommonUtil.setPref(mContext, CommonUtil.GCMREGID, regid);
	            }
	        }.execute(null, null, null);
	    }
	  
	  @SuppressLint("NewApi")
	public static void showNotification(Context context,String title,String content){
		  /*String url="http://162.220.160.135/ww/login.asp";
		  Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));*/
		  Intent myIntent = new Intent(context, MainActivity.class);
		    PendingIntent pendingIntent = PendingIntent.getActivity(
		    context, 
		      0, 
		      myIntent, 
		      Intent.FLAG_ACTIVITY_NEW_TASK);
		  try {
			title=new String(title.getBytes("UTF-8"));
			content=new String(content.getBytes("UTF-8"));
		  } catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  Notification builder=new Notification.Builder(context)
		  .setContentTitle(title)
          .setContentText(content)
          .setTicker(context.getResources().getString(R.string.app_name))
          .setWhen(System.currentTimeMillis())
          .setContentIntent(pendingIntent)
          .setDefaults(Notification.DEFAULT_SOUND)
          .setAutoCancel(true)
          .setSmallIcon(R.drawable.ic_launcher)
          .build();
		  
		  NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		    notificationManager.notify(notifyid, builder);
		    notifyid++;
		  
	  }
	  
}
